package com.xushi.service.impl;

import com.xushi.bean.State;
import com.xushi.dao.UserTemplateRepository;
import com.xushi.model.Template;
import com.xushi.model.UserTemplate;
import com.xushi.service.UserTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;

@Service("userTemplateService")
@Transactional
public class UserTemplateServiceImpl implements UserTemplateService {
    @Autowired
    private UserTemplateRepository userTemplateDao;

    public UserTemplate createUserTemplate(String username, Template template, Timestamp startTime, Timestamp endTime) {
        UserTemplate userTemplate = new UserTemplate();
        userTemplate.setUsername(username);
        userTemplate.setTemplate(template);
        userTemplate.setStartTime(startTime);
        userTemplate.setEndTime(endTime);
        userTemplate.setState(State.ACTIVE.getState());
        return userTemplateDao.save(userTemplate);
    }

    @Override
    public void setUserTemplate(int userTemplateId, String title, int playCount, String description, String description2, String weixinQrCodePath) {
        UserTemplate userTemplate = userTemplateDao.findUserTemplateById(userTemplateId);

        userTemplate.setDescription(description);
        userTemplate.setPlayCount(playCount);
        userTemplate.setTitle(title);
        userTemplate.setDescription2(description2);
        if (!("".equals(weixinQrCodePath))) {
            userTemplate.setWeixinQrCodePath(weixinQrCodePath);
        }

        userTemplateDao.save(userTemplate);
    }

    @Override
    public List<UserTemplate> getUserTemplateList(String username) {
        List<UserTemplate> userTemplateList = userTemplateDao.findUserTemplatesByUsername(username);
        return userTemplateList;
    }

    @Override
    public UserTemplate getUserTemplateById(int userTemplateId) {
        return userTemplateDao.findUserTemplateById(userTemplateId);
    }

    @Override
    public List<UserTemplate> getUserTemplateListByTemplate(Template template) {
        return userTemplateDao.findUserTemplatesByTemplate(template);
    }
}
